package com.itheima.web.service.impl.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.service.ums.ResourceService;
import com.itheima.utils.PageResult;
import com.itheima.utils.ResultData;
import com.itheima.vo.ums.UmsResource;
import com.itheima.web.mapper.ums.ResourceMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.HashMap;

@DubboService
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, UmsResource> implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 添加后台资源
     * @param resource
     * @return
     */
    @Override
    public ResultData insert(UmsResource resource) {
        resource.setCreateTime(LocalDateTime.now());
        this.save(resource);
        return ResultData.ok("","");
    }

    /**
     * 分页模糊查询后台资源
     * @param categoryId
     * @param nameKeyword
     * @param pageNum
     * @param pageSize
     * @param urlKeyword
     * @return
     */
    @Override
    public ResultData listPage(Integer categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword) {
        Page<UmsResource> page = new Page<>(pageNum , pageSize );
        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        lqw.eq( categoryId != null ,UmsResource::getCategoryId ,categoryId)
                .like(StringUtils.isNotEmpty(nameKeyword) , UmsResource::getName ,nameKeyword)
                .like(StringUtils.isNotEmpty(urlKeyword) , UmsResource::getUrl ,urlKeyword);

        this.page(page, lqw);
        //return ResultData.ok("OK", PageResult.init(page.getCurrent() , pageNum.longValue() , pageSize.longValue() ,page.getRecords() ));
        PageResult result = PageResult.getPage(page);

        return ResultData.ok("",result);
    }
}
